/*  Implementation of Unix/Linux tail command.
 * 	Output the last part of files. 
 * 	With more than one FILE, precede each with a header giving the file  name.
 *  With no FILE, or when FILE is -, read standard input.

    Copyright (C) 2012  Vasil Kostov <noobed>
    For any questions or advices don't hesitate to contact me - noobed7@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

package tail;

/**
 * This class provides mechanism for keeping last <i>N</i> characters when
 * reading from stdin. You could imagine this as a ring with a length of
 * <i>N</i> - when you put the <i>N-th<i/> character the <i>N+1-th</i> will
 * replace the first.
 * 
 * @author Vasil Kostov <noobed>
 * @version 1.0 BETA
 * 
 */
public class CharControl {

	private char[] flow;
	private int size;
	private int count;

	/**
	 * A "<i>buffer</i>" is created with a specific size.
	 * 
	 * @param size
	 *            - the size of the buffer.
	 */
	public CharControl(int size) {
		this.size = size;
		count = 0;
		flow = new char[size];
	}

	/**
	 * push does similar work to : <i>buffer</i>[count++ % size] = c
	 * 
	 * 
	 * @param c
	 *            - the character to be pushed back.
	 */
	public void push(char c) {
		flow[count++ % size] = c;
	}

	/**
	 * prints the "<i>buffer</i>". If less than size elements are pushed - print
	 * will start from first element. Otherwise it'll print beginning on the
	 * element after the last pushed.
	 */
	public void print() {
		if (count < size)
			for (int i = 0; i < size; i++)
				System.out.print(flow[i]);
		else
			for (int i = 0; i < size; i++)
				System.out.print(flow[(count + i) % size]);
	}
}
